package com.daoxiang.controller;

import com.daoxiang.pojo.Administrator;
import com.daoxiang.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class AdministratorController {

    @Autowired
    private AdministratorService administratorService;

    /**
     * 用实体类接收前端传过来的用户名和密码，在数据库中查询并验证，整个登录到主页面，失败返回登录页面
     * @Author wml
     * @param administrator
     * @return ModelAndView
     */
    @GetMapping("/backstage_login")
    public ModelAndView doLogin(Administrator administrator) {
        // 返回对象
        ModelAndView modelAndView = new ModelAndView();
        // 调用service层进行登录业务逻辑处理
        List<Administrator> administratorList =  this.administratorService.queryAdministratorByAccountAndPassword(administrator);
        // 判断是否登录成功
        if(administratorList != null && administratorList.size() > 0) {
            // 登录成功
            // 缓存登录成功的数据
            modelAndView.addObject("loginAdministrator",administratorList.get(0));
            // 返回登录成功的视图名
            modelAndView.setViewName("backstage/login/main");
        } else {
            // 登录失败，返回登录页面
            modelAndView.setViewName("backstage/index");
        }
        // 返回视图
        return modelAndView;
    }

    @GetMapping("/seller_login")
    public ModelAndView doSellerLogin(Administrator administrator) {
        // 返回对象
        ModelAndView modelAndView = new ModelAndView();
        // 调用service层进行登录业务逻辑处理
        List<Administrator> administratorList =  this.administratorService.queryAdministratorByAccountAndPassword(administrator);
        // 判断是否登录成功
        if(administratorList != null && administratorList.size() > 0) {
            // 登录成功
            // 缓存登录成功的数据
            modelAndView.addObject("loginAdministrator",administratorList.get(0));
            // 返回登录成功的视图名
            modelAndView.setViewName("seller/login/main");
        } else {
            // 登录失败，返回登录页面
            modelAndView.setViewName("seller/index");
        }
        // 返回视图
        return modelAndView;
    }
}
